using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Express.Core.Responses;
using Express.Tools;

namespace Express.Core.Requests
{
    public class KDNiaoHelper
    {
        //电商ID,请到快递鸟官网申请http://www.kdniao.com/ServiceApply.aspx
        private const string EBusinessID = "25365463";
        //电商加密私钥，快递鸟提供，注意保管，不要泄漏
        private const string AppKey = "*******************************";

        /// <summary>
        /// Json方式  单号识别
        /// </summary>
        /// <returns></returns>
        public async Task<OrderDisResponse> OrderTracesSubByJson(string expressId = "710386923334")
        {
            string response = string.Empty;
            try
            {
                //string reqUrl = "http://testapi.kdniao.cc:8081/Ebusiness/EbusinessOrderHandle.aspx";
                //正式
                string reqUrl = "http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";
                string requestData = "{'LogisticCode': '" + expressId + "'}";
                Dictionary<string, string> param = new Dictionary<string, string>();
                param.Add("RequestData", requestData);
                param.Add("EBusinessID", EBusinessID);
                param.Add("RequestType", "2002");
                string dataSign = SignHelper.Encrypt(requestData, AppKey, "UTF-8");
                param.Add("DataSign", dataSign);
                param.Add("DataType", "2");
                response = await RestService.SendPost(reqUrl, param);
                var result = JsonConvert.DeserializeObject<OrderDisResponse>(response);
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 查询快递单物流轨迹
        /// </summary>
        /// <param name="exType"></param>
        /// <param name="exCode"></param>
        /// <returns></returns>
        public async Task<OrderTracesResponse> GetOrderTracesByJson(string exType,string exCode)
        {
            try
            {
                string reqUrl = "http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";
                string requestData = "{'OrderCode':'','ShipperCode':'" + exType + "','LogisticCode':'" + exCode + "'}";

                Dictionary<string, string> param = new Dictionary<string, string>();
                param.Add("RequestData", requestData);
                param.Add("EBusinessID", EBusinessID);
                param.Add("RequestType", "1002");
                string dataSign = SignHelper.Encrypt(requestData, AppKey, "UTF-8");
                param.Add("DataSign", dataSign);
                param.Add("DataType", "2");

                var result = await RestService.SendPost(reqUrl, param);
                var jsonObject = JsonConvert.DeserializeObject<OrderTracesResponse>(result);
                return jsonObject;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
